package com.hsmw.api.controller.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.service.HsmwAuditService;
import com.hsmw.api.service.HsmwVehicleService;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.HsmwAudit;
import com.hsmw.domain.HsmwVehicle;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author WD
 */
@Api(tags = "BIZ-司机审核管理")
@RestController
@RequestMapping("/api/audit")
public class BizAuditController extends BaseController<HsmwAuditService, HsmwAudit> {

	@Autowired
	private HsmwAuditService hsmwAuditService;


	@Autowired
	private HsmwVehicleService hsmwVehicleService;


	/**
	 * 分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取司机分页列表，参数： 审核状态 auditStatus  审核中、审核通过、审核拒绝 ；司机姓名或者车牌号：queryParam；提交时间：createTime  格式：yyyy-MM-dd HH:mm:ss  ", notes = "获取司机分页列表", produces = "application/json")
	@PostMapping("/getHsmwAuditPage")
	public DataResponse getHsmwAuditPage(@RequestBody @Valid BasePage<HsmwAudit> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwAudit hsmwAudit = basePage.getRequestData();
		IPage<HsmwAudit> data = hsmwAuditService.getHsmwAuditPage(page, hsmwAudit);
		for (HsmwAudit record : data.getRecords()) {
			String vehiclePlate = record.getVehiclePlate();
			List<HsmwVehicle> vehicleList = hsmwVehicleService.list(new LambdaQueryWrapper<HsmwVehicle>()
					.eq(HsmwVehicle::getVehiclePlate,vehiclePlate));
			boolean isExists =false;
			if(CollectionUtil.isNotEmpty(vehicleList)){
				isExists=true;
			}
			record.setExists(isExists);
		}
		return DataResponse.success(data);
	}

	/**
	 * 根据ID查询数据
	 *
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "根据id查询司机数据", notes = "根据id查询数据", produces = "application/json")
	@PostMapping("/getHsmwAuditById")
	public DataResponse getHsmwAuditById(@RequestBody GetObjectByIdForm form) {
		HsmwAudit hsmwAudit = hsmwAuditService.getHsmwAuditById(form.getKey());
		List<HsmwVehicle> vehicleList = hsmwVehicleService.list(new LambdaQueryWrapper<HsmwVehicle>()
				.eq(HsmwVehicle::getVehiclePlate,hsmwAudit.getVehiclePlate()));
		boolean isExists =false;
		if(CollectionUtil.isNotEmpty(vehicleList)){
			isExists=true;
		}
		hsmwAudit.setExists(isExists);
		return new DataResponse(hsmwAudit);
	}

	/**
	 * 修改司机审核信息
	 *
	 * @param audit
	 * @return
	 */
	@ApiOperation(value = "中控端 修改司机审核信息", notes = "修改司机审核信息", produces = "application/json")
	@PostMapping("/upadteHsmwAudit")
	public DataResponse upadteHsmwAudit(@RequestBody HsmwAudit audit) {
		try {
			hsmwAuditService.updateById(audit);
			return new DataResponse();
		}catch (Exception e){
			return  DataResponse.failure(CommonConstants.ResponseStatus.VALIDATE_FAILED.getCode(),"司机审核信息修改失败，请稍后重试！");
		}
	}


	/**
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "司机审核通过", notes = "审核通过", produces = "application/json")
	@PostMapping("/passHsmwAudit")
	public DataResponse passHsmwAudit(@RequestBody GetObjectByIdForm form) {
		return hsmwAuditService.passHsmwAudit(form.getKey());
	}

	/**
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "司机 审核拒绝", notes = "审核拒绝  id、remark：备注", produces = "application/json")
	@PostMapping("/refuseHsmwAudit")
	public DataResponse refuseHsmwAudit(@RequestBody JSONObject form) {
		hsmwAuditService.refuseHsmwAudit(form.getStr("key"), form.getStr("remark"));
		return new DataResponse(true);
	}








}
